// JHUB1 Ajax processing 
// JHUB1.org 2012
// marek kaszewski jhub1@jhub1.org

function Jhub1 () {
    this.currentUri = "";
    this.showAllNewHubs = 0;
    this.newHubs = 0;
    this.awaitingHubs = 0;
    this.controller = "";
    this.action = "";
    this.newUSBHubs = "";
    this.systemStatus = "";
    this.uriContent = "";
};

Jhub1.prototype.getInfo = function() {
    return 1;
};

Jhub1.prototype.processObject = function (obj) {
    var jsonObj = typeof obj != 'object' ? JSON.parse(obj) : obj;
    if (jsonObj['NewUSBHubs']) {
     //   console.log('NewUSBHubs');
        this.newUSBHubs = jsonObj['NewUSBHubs'];
        this.countRecords('new');
        this.countRecords('awaiting');
        console.log('new ' + this.newHubs + ' awaiting: ' + this.awaitingHubs);
    }
    if (jsonObj[this.currentUri]) {
        this.uriContent = jsonObj[this.currentUri];
        console.log(this.currentUri);
    }
    if (jsonObj['SystemStatus']) {
        console.log('SystemStatus');
        this.systemStatus = jsonObj['SystemStatus'];
    }   
};

Jhub1.prototype.countRecords = function (type) {
    var counter = 0;
    for (var i = 0; i < this.newUSBHubs.length; i++) {
        for (var index in this.newUSBHubs[i]) {
            if (index == "status") {
                if (this.newUSBHubs[i][index] == type) {
                    counter++;
                }
            }
        }
    }
    if (type == 'new') {
        this.newHubs = counter;
    } else if (type == 'awaiting') {
        this.awaitingHubs = counter;
    }
};

Jhub1.prototype.createNewHubsInfo = function () { 
    var str = '';
    var statusType = 'awaiting';
    if (this.showAllNewHubs) {
        statusType = '';
    }
    for (var i = 0; i < this.newUSBHubs.length; i++) {
        if (this.newUSBHubs[i]['status'] != statusType) {
            str += (i % 2 == 0) ? '<tr class="even" id="row-' + this.newUSBHubs[i].dev + '">' : '<tr id="row-' + this.newUSBHubs[i].dev + '">';
            str += '<td class="checkbox-cell">' + (i+1) + '</td>';
            var dev = "";
            for (var index in this.newUSBHubs[i]) {
                if (index != "status") {
                    var cssclass = '';
                    if (index != "dev") {
                        cssclass = 'class="center-cell" id="' + this.newUSBHubs[i].dev + '"';
                    } else if (index == "dev") {
                        dev = this.newUSBHubs[i][index];
                    }
                    str += '<td ' + cssclass + '>' + this.newUSBHubs[i][index] + '</td>';
                }
            }
            str += '<td class="action-cell center-cell">';
            str += '<a href="/hubs/addusbhub/usbhubid/' + dev + '" title="Install now!"><img src="/images/icons/page_white_edit.png" alt="" title="" width="16" height="16"/></a>';
            str += '<a title="Install later, do not remind me"><img id="' + dev + '" class="setAwaitingButton" src="/images/icons/cross.png" alt="" title="" width="16" height="16"/></a>';
            str += '</td>';
            str += '</tr>';
            this.populateContent(str, this.newUSBHubs[i].dev, this.newUSBHubs[i].date);
            str = "";
        } 
    }
   // return str;
};

Jhub1.prototype.populateContent = function (str, dev, idate) {
    //console.log(str);
    if($("#" + dev).length > 0) {
        $("#" + dev).html(idate);
    } else {
        $('#newUSBHubsContent').append(str).fadeIn('slow');
    }  
}